package com.hnqcgczy.supplier.controller;

import com.hnqcgczy.supplier.service.CompanyService;
import com.hnqcgczy.supplier.service.impl.CompanyServiceImpl;
import com.hnqcgczy.supplier.utils.BaseServlet;
import com.hnqcgczy.supplier.vo.Company;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Date;
import java.sql.SQLException;
import java.util.List;

@WebServlet(name = "CompanyServlet", value = "/CompanyServlet")
public class CompanyServlet extends BaseServlet {
    //多态
    private CompanyService companyService = new CompanyServiceImpl();

    //添加
    public void addCompany(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        Company company = new Company();
        company.setCompanyId(Integer.valueOf(request.getParameter("company_id") != null ? request.getParameter("company_id") : "0"));
        company.setCompanyName(request.getParameter("company_name") != null ? request.getParameter("company_name") : "");
        company.setCompanyShortName(request.getParameter("company_short_name"));
        company.setCompanyEnglishName(request.getParameter("company_english_name"));
        company.setRegistrationNumber(request.getParameter("registration_number"));
        company.setRegisteredFund(Double.parseDouble(request.getParameter("registered_fund") != null ? request.getParameter("registered_fund") : "0"));
        company.setLegalPersonName(request.getParameter("legal_person_name"));
        company.setStaffNumber(Integer.valueOf(request.getParameter("staff_number") != null ? request.getParameter("staff_number") : "0"));
        company.setEstablishmentDate(Date.valueOf(request.getParameter("establishment_date")));
        company.setMainServicesRange(request.getParameter("main_services_range"));
        company.setOtherServicesRange(request.getParameter("other_services_range"));
        company.setRegisteredAddress(request.getParameter("registered_address"));
        company.setWorkAddress(request.getParameter("work_address"));
        company.setWorkAddressEmail(request.getParameter("work_address_email"));
        company.setServicesAddressRange(request.getParameter("services_address_range"));
        company.setQualityCommitment(request.getParameter("quality_commitment"));
        company.setPhone(request.getParameter("phone"));
        company.setFaxNumber(request.getParameter("fax_number"));
        company.setCompanyUrl(request.getParameter("company_url"));
        company.setReactionSpeedCommitment(request.getParameter("reaction_speed_commitment"));
        company.setCompanyAdvantage(request.getParameter("company_advantage"));
        company.setCompanyPartner(request.getParameter("company_partner"));
        company.setCompanyDispute(request.getParameter("company_dispute"));
        //调添加
        boolean flag = companyService.addCompany(company);
        //跳转
        response.sendRedirect("CompanyServlet");
    }

    //查
    public void queryAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        String companyName = request.getParameter("companyName");
        String code = request.getParameter("code");
        String companyId = request.getParameter("company_id") != null ? request.getParameter("company_id") : "0";
        //获取当前页
        Integer pageNow = Integer.parseInt(request.getParameter("pageNow") != null ? request.getParameter("pageNow") : "1");


        //获取每页显示的大小
        Integer pageSize = 10;

        //封装到实体
        Company temp = new Company();
        temp.setCompanyName(companyName);
        temp.setRegistrationNumber(code);
        temp.setCompanyId(Integer.parseInt(companyId));
        //获取总页数
        int totals = companyService.queryTotals(temp);
        //算出总页数，有10条数据，每页显示3条，四页。。。有9条数据，3页
        int pages = totals % pageSize == 0 ? totals / pageSize : totals / pageSize + 1;
        if (pageNow < 1) {
            pageNow = 1;
        }
        if (pageNow > pages) {
            pageNow = pages;
        }
        List<Company> companies = companyService.queryByLike(temp, pageNow, pageSize);
        //传值
        request.getSession().setAttribute("companies", companies);
        request.setAttribute("pageNow", pageNow);
        request.setAttribute("pages", pages);
        request.setAttribute("totals", totals);
        //跳转
        request.getRequestDispatcher("BasicInformation.jsp").forward(request, response);

    }

    //更新
    public void updateById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        Company company = new Company();
        company.setCompanyId(Integer.valueOf(request.getParameter("company_id") != null ? request.getParameter("company_id") : "0"));
        company.setCompanyName(request.getParameter("company_name") != null ? request.getParameter("company_name") : "");
        company.setCompanyShortName(request.getParameter("company_short_name"));
        company.setCompanyEnglishName(request.getParameter("company_english_name"));
        company.setRegistrationNumber(request.getParameter("registration_number"));
        company.setRegisteredFund(Double.parseDouble(request.getParameter("registered_fund") != null ? request.getParameter("registered_fund") : "0"));
        company.setLegalPersonName(request.getParameter("legal_person_name"));
        company.setStaffNumber(Integer.valueOf(request.getParameter("staff_number") != null ? request.getParameter("staff_number") : "0"));
        company.setEstablishmentDate(Date.valueOf(request.getParameter("establishment_date")));
        company.setMainServicesRange(request.getParameter("main_services_range"));
        company.setOtherServicesRange(request.getParameter("other_services_range"));
        company.setRegisteredAddress(request.getParameter("registered_address"));
        company.setWorkAddress(request.getParameter("work_address"));
        company.setWorkAddressEmail(request.getParameter("work_address_email"));
        company.setServicesAddressRange(request.getParameter("services_address_range"));
        company.setQualityCommitment(request.getParameter("quality_commitment"));
        company.setPhone(request.getParameter("phone"));
        company.setFaxNumber(request.getParameter("fax_number"));
        company.setCompanyUrl(request.getParameter("company_url"));
        company.setReactionSpeedCommitment(request.getParameter("reaction_speed_commitment"));
        company.setCompanyAdvantage(request.getParameter("company_advantage"));
        company.setCompanyPartner(request.getParameter("company_partner"));
        company.setCompanyDispute(request.getParameter("company_dispute"));
        //调修改
        boolean flag = companyService.update(company);
        //跳转
        response.sendRedirect("CompanyServlet");
    }

    //删除
    public void deleteById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        Integer id = Integer.parseInt(request.getParameter("company_id") != null ? request.getParameter("company_id") : "0");
        boolean flag = companyService.delete(id);
        //跳转
        response.sendRedirect("CompanyServlet");
    }
}
